package util

import (
	"bufio"
	"fmt"
	"github.com/fatih/color"
	"os"
	"strconv"
	"strings"
)

func StrToInt(str string) int {
	int, _ := strconv.Atoi(str)
	return int
}

func StrToInt64(str string) int64 {
	int64, _ := strconv.ParseInt(str, 10, 64)
	return int64
}

func IntToStr(int int) string {
	return strconv.Itoa(int)
}

func IsInStrSlice(sliceParam []string, param string) bool {
	for _, v := range sliceParam {
		if v == param {
			return true
		}
	}
	return false
}

func WaitInput(msg string) string {
	input := bufio.NewScanner(os.Stdin)
	fmt.Println(msg)
	fmt.Printf("请输入: ")
	var text string
	for input.Scan() {
		inputVal := input.Text()
		if inputVal != "" {
			text = input.Text()
			break
		}
		fmt.Printf("请输入: ")
	}
	return text
}

func WaitInputEnter(msg string) string {
	reader := bufio.NewReader(os.Stdin)
	fmt.Println(msg)
	fmt.Print("请输入(按回车可跳过): ")
	text, _ := reader.ReadString('\n')
	text = strings.Replace(text, "\n", "", -1)
	return text
}

func WaitInputEndStr(msg string) string {
	input := bufio.NewScanner(os.Stdin)
	fmt.Println(msg)
	fmt.Println("请输入内容，输入`:q`表示结束 ")
	var text string
	for input.Scan() {
		inputVal := input.Text()
		if inputVal == ":q" {
			break
		}
		text += inputVal + "\n"
	}
	return text
}

func Dump(i ...interface{}) {
	fmt.Print("debug:")
	Print(i)
}

func Print(i ...interface{}) {
	fmt.Println(i...)
}

func ColorPrintLn(attr color.Attribute, msg string) {
	c := color.New(attr)
	c.Println(msg)
}

func ColorPrint(attr color.Attribute, msg string) {
	c := color.New(attr)
	c.Print(msg)
}

func CheckFileIsExist(filename string) bool {
	var exist = true
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		exist = false
	}
	return exist
}
